package eu.szemraj.display.ui.scroller {
	import eu.szemraj.display.DisposableSprite;
	import eu.szemraj.display.ui.Button;
	import eu.szemraj.utils.MathUtil;

	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	/**
	 * @author sema
	 */
	public class Scroller extends DisposableSprite {

		
		public var thumb:Button;
		public var track:Button;

		public var scrollUp:Button;		public var scrollDown:Button;
		

		public function Scroller() {
		}	

		public function update( scrollPercentage:Number ):void {
			
			scrollPercentage = MathUtil.clamp( scrollPercentage, 0, 1 );
			var rect:Rectangle = thumbDragRectangle;
			thumbY = scrollPercentage * rect.height + rect.top;
		}

		public function get scrollPercentage():Number {
						var rect:Rectangle = thumbDragRectangle;
			return ( thumb.y - rect.top ) / rect.height;
		}

		public function initialize():void {
			
			initializeThumb( );
			initializeButtons();
			initializeTrack();
		}

		override public function dispose():void {
			
			thumb.dispose( );
			super.dispose( );
		}
		
		private function set thumbY( value:Number ):void {
			
			var rect:Rectangle = thumbDragRectangle;
			thumb.y = int( MathUtil.clamp( value, rect.top, rect.bottom ) );
			onScroll();
			
		}

		private function initializeButtons():void {
			
			if( scrollUp ) {
				
				scrollUp.addEventListener( MouseEvent.MOUSE_DOWN, buttonMouseEventHandler );
			}
			if( scrollDown ) {
				
				scrollDown.addEventListener( MouseEvent.MOUSE_DOWN, buttonMouseEventHandler );
			}
		}
		
		private function initializeTrack():void {
			
			if( track ) {
				
				track.addEventListener( MouseEvent.CLICK, trackClick );
			}
		}
		
		private function trackClick(event:MouseEvent):void {
			
			var rect:Rectangle = thumbDragRectangle;
			thumbY = rect.top + ( track.mouseY / track.height ) * rect.height;
		}

		private function buttonMouseEventHandler(event:MouseEvent):void {
			
			var thumbYPosition:Number = thumb.y;			var rect:Rectangle = thumbDragRectangle;
			if( event.target == scrollUp ){
				
				thumbYPosition -= rect.height / 10;
			}
			if( event.target == scrollDown ) {
				
				thumbYPosition += rect.height / 10;
			}
			thumbY = thumbYPosition;
		}

		private function initializeThumb():void {
			
			if( thumb ) {			
				thumb.addEventListener( MouseEvent.MOUSE_DOWN, thumbMouseEventHandler );
				thumb.useHandCursor = thumb.buttonMode = true;
			}
		}

		private function thumbMouseEventHandler(event:MouseEvent):void {
			
			
			switch( event.type ) {
				
				
				case MouseEvent.MOUSE_DOWN :
				
					thumb.removeEventListener( event.type, thumbMouseEventHandler );
					thumb.startDrag( false, thumbDragRectangle );
					stage.addEventListener( MouseEvent.MOUSE_MOVE, thumbMouseEventHandler );
					stage.addEventListener( MouseEvent.MOUSE_UP, thumbMouseEventHandler );
					
					break;
				
				case MouseEvent.MOUSE_UP :
				
					thumb.stopDrag( );
					stage.removeEventListener( event.type, thumbMouseEventHandler );
					stage.removeEventListener( MouseEvent.MOUSE_MOVE, thumbMouseEventHandler );
					thumb.addEventListener( MouseEvent.MOUSE_DOWN, thumbMouseEventHandler );
					
					break;
				
				case MouseEvent.MOUSE_MOVE :
				
					onScroll( );
					
					break;	
			}
		}

		
		private function get thumbDragRectangle():Rectangle {
			
			var rect:Rectangle = new Rectangle();
			rect.left = 0;
			rect.right = 0;
			rect.top = scrollUp.y + scrollUp.height;
			rect.bottom = scrollDown.y - thumb.height;
            return rect;
		}

		
		private function onScroll():void {
			
			dispatchEvent( new ScrollerEvent( scrollPercentage ) );			
		}
	}
}
